/**
 * 
 */
package com.cipe.cmrs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.Country;
import com.cipe.cmrs.model.State;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;

/**
 * @author Incepio
 * 
 */
@Service
@Transactional
public class StateService extends BaseService<State> {

	@Autowired
	private UserService	userService;

	public State saveState(State state, Integer userId) {
		if (state != null) {
			User user = userService.findById(userId);
			State dbState = findById(state.getId());
			if (dbState == null) {
				dbState = new State();
				dbState.setCreatedBy(user);
				dbState.setCreationDate(DateUtil.getCurrentDate());
			}
			Country country = state.getCountry();
			if (null != country) {
				dbState.setCountry(country);
			}
			dbState.setDisplayName(state.getDisplayName());
			dbState.setUpdatedBy(user);
			dbState.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbState);
		}
		return null;
	}
}
